#include <signal.h>
#include <aura.h>

void ThreadEntry(Aura_PVoid pArg)
{
    Aura_Handle hEvent = (Aura_Handle)pArg;
    Aura_WaitResult wr;

    Aura_printf("child thread: start ..\n");
    for (Aura_Int32 i = 0; i < 5; i++) {
        Aura_printf(" %d .", i);
        AuraThread_Sleep(500, &wr);
    }
    Aura_ECode ec = AuraEvent_Signal(hEvent,AURA_NULL);
    if (AURA_FAILED(ec)) {
        Aura_printf("child thread: failed to siganl event. (ec = 0x%x)\n",ec);
    }
    Aura_printf("child thread: signal\n");
    for (Aura_Int32 i = 0; i < 500; i++) {
        Aura_printf(" %d .", i);
        AuraThread_Sleep(500, &wr);
    }
    Aura_printf("\nchild thread: end..\n");
    return;
}


Aura_Bool AuraTest_Event()
{
    Aura_ThreadId threadId = AURA_INVALID_TID;
    Aura_Handle hThread = AURA_NULL;
    Aura_ECode ec = AURA_NOERROR;
    Aura_Handle hEvent = AURA_NULL;
    Aura_WaitResult wr;




    Aura_printf("father: test start...\n");
    ec = AuraEvent_Create(AURA_FALSE, 0, &hEvent);
    if (AURA_FAILED(ec)) {
        Aura_printf("father: failed to create event. \n");
        return AURA_FALSE;
    }

    ec = AuraThread_Create(ThreadEntry, hEvent, 0, &hThread, &threadId);
    if (AURA_FAILED(ec)) {
        Aura_printf("fahter: failed to create thread.\n");
        return AURA_FALSE;
    }

    Aura_printf("fahter: begin wait\n");
    ec = AuraEvent_Wait(hEvent, 5000, &wr, AURA_NULL);
    if (AURA_FAILED(ec)) {
        Aura_printf("fahter: failed to wait (ec = 0x%x)\n",ec);
        return AURA_FALSE;
    }
    Aura_printf("\nfather: wr = %d",wr);
    AuraEvent_Destroy(hEvent);
    Aura_printf("\nfather: test end. ");

    return AURA_TRUE;
}
